package praktikumid.w11.p1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class StringsUndoCopy {
	static class Element {
		String name;
		
		public Element(Element e) {
			name = e.name;
		}
		
		public Element(String name) {
			this.name = name;
		}
		@Override
		public String toString() {
			return name;
		}
	}
	
	
	
	static List<Element> elements = new ArrayList<Element>();
	
	static List<List<Element>> history = new ArrayList<List<Element>>();
	static int historyIndex = 0;
	
	
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		while(true) {
			/*
			 * 1 tere
			 * tokens[0] = "1"
			 * tokens[1] = "tere"
			 * 
			 * op = 1
			 * new Element("tere");
			 * 
			 */
			printHelp();
			String line = br.readLine();
			String[] tokens = line.split(" ");
			int op = Integer.valueOf(tokens[0]);
			int elid = -1;
			if (op > 0 && op < 4) {
				addHistory();
			}
			switch (op) {
			case 1:
				elements.add(new Element(tokens[1]));
				break;
			case 2: // rename
				// tokens[1] -> index
				elid = Integer.valueOf(tokens[1]);
				Element e = elements.get(elid);
				e.name = tokens[2];
				break;
			case 3: // delete
				elid = Integer.valueOf(tokens[1]);
				elements.remove(elid);
				break;
			case 4:
				printElements();
				break;
			case 5:
				undo();
				break;
			case 0:
				System.exit(0);
				
			}
			
		}
	}
	
	public static void addHistory() {
		List<Element> newList = new ArrayList<Element>();
		for (Element e : elements) {
			newList.add(new Element(e));
			// same as:
			//Element e2 = new Element();
			//e2.name = e.name;
			//newList.add(e2);
		}
		history.add(historyIndex++, newList);
	}
	
	public static void undo() {
		elements = history.get(--historyIndex);
	}
	
	public static void printElements() {
		for (int i = 0; i < elements.size(); i++) {
			System.out.println(i + ". " + elements.get(i));
		}
	}
	
	public static void printHelp() {
		System.out.println("  1 add element [1 name]");
		System.out.println("  2 change element [2 elid name]");
		System.out.println("  3 delete element [3 elid]");
		System.out.println("  4 list elements [4]");
		System.out.println("  5 undo [5]");
		System.out.println("  0 exit [0]");
	}
}